package com.srd.phone.utils.wheel.widget;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.shengrongdai.srd.R;
import com.srd.phone.utils.wheel.adapter.ArrayWheelAdapter;

/**
 * 2016 - 05- 16 3
 */
public class DialogUtils extends BaseDialog implements View.OnClickListener, OnWheelChangedListener {

    private Context mContext;
    private WheelView wvProvince, wvCity, wvDistrict;
    private OnAddressCListener mOnAddressCListener;

    public DialogUtils(Context context, OnAddressCListener onAddressCListener) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.mOnAddressCListener = onAddressCListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_dialog_util);

        setUpViews();
        setUpListener();
        setUpData();

    }

    private void setUpViews() {
        wvProvince = (WheelView) findViewById(R.id.wv_province);
        wvCity = (WheelView) findViewById(R.id.wv_city);
        wvDistrict = (WheelView) findViewById(R.id.wv_district);

        findViewById(R.id.btn_myinfo_sure).setOnClickListener(this);
        findViewById(R.id.btn_myinfo_cancel).setOnClickListener(this);

    }

    private void setUpListener() {
        wvProvince.addChangingListener(this);
        wvCity.addChangingListener(this);
        wvDistrict.addChangingListener(this);
    }

    private void setUpData() {
        initDatas();
        wvProvince.setViewAdapter(new ArrayWheelAdapter<String>(mContext, mProvinceDatas));
        wvProvince.setVisibleItems(0);
        wvCity.setVisibleItems(0);
        wvDistrict.setVisibleItems(0);
        updateCities();
        updateAreas();
    }

    private void updateAreas() {
        int pCurrent = wvCity.getCurrentItem();
        mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
        String[] areas = mDistrictDatasMap.get(mCurrentCityName);

        if (areas == null) {
            areas = new String[]{""};
        }
        wvDistrict.setViewAdapter(new ArrayWheelAdapter<String>(mContext, areas));
        wvDistrict.setCurrentItem(0);
        mCurrentDistrictName = areas[0];
    }

    private void updateCities() {
        int pCurrent = wvProvince.getCurrentItem();
        mCurrentProviceName = mProvinceDatas[pCurrent];
        String[] cities = mCitisDatasMap.get(mCurrentProviceName);
        if (cities == null) {
            cities = new String[]{""};
        }
        wvCity.setViewAdapter(new ArrayWheelAdapter<String>(mContext, cities));
        wvCity.setCurrentItem(0);
        updateAreas();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_myinfo_sure:
                if (mOnAddressCListener != null)
                    mOnAddressCListener.onClick(mCurrentProviceName, mCurrentCityName, mCurrentDistrictName);
                dismiss();
                break;

            case R.id.btn_myinfo_cancel:
                dismiss();
                break;
        }
    }

    public void dismiss() {
        mContext = null;
        super.dismiss();
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == wvProvince) {
            updateCities();
        } else if (wheel == wvCity) {
            updateAreas();
        } else if (wheel == wvDistrict) {
            mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[newValue];
            mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);
        }
    }

    public interface OnAddressCListener {
        public void onClick(String provinceName, String cityName, String districtName);
    }
}
